//
//  ZKTabBarController.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/22.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

class ZKTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //只读属性不能直接赋值
        //self.tabBar = ZKTabBar()
        
        //使用KVC给只读属性赋值,本质是给只读属性的成员变量赋值
        let myTabBar = ZKTabBar()
        
        self.setValue(myTabBar, forKey: "tabBar")
        
        //设置闭包
        //这里需要注意循环引用
        myTabBar.composeButtonClosure = { [unowned self] in
            
            //定义闭包
            print("闭包回调过来的")
            //设置代理
            myTabBar.zkdelegate = self
            
            let composeView = ComposeView()
            
            composeView.show(target: self)
            
        }
        
        
        
        
        addChildViewController(childController: ZKHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(childController: ZKMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(childController: ZKDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(childController: ZKMineTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

    
    //MARK: -------重载添加子视图-------
    func addChildViewController(childController: UIViewController,title:String,imageName:String) {
        
        //1.设置标题
        //childController.tabBarItem.title = title
        childController.title = title
        
        //2.设置图片,并设置颜色
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named:imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //修改文字选中状态的颜色和文字大小
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont .systemFont(ofSize: 12)], for: .normal)
        
        //3.创建导航视图控制器
        let nav = ZKNavgationController(rootViewController: childController)
        
        //4.添加子视图控制器
        addChildViewController(nav)
    }
    
 


}

//MARK: -------ZKTabBarDelegate-------
extension ZKTabBarController:ZKTabBarDelegate{
    
    func cilickcomposeButtonOperation() {
        
        print("代理回调")
    }
    
    
}
